package doorwifi.wd.patientsation.peripheral.mainapp.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

import doorwifi.wd.patientsation.peripheral.mainapp.Bean.CallLineUpInfo;
import doorwifi.wd.patientsation.peripheral.mainapp.Bean.illnessRoom;
import doorwifi.wd.patientsation.peripheral.mainapp.R;
import doorwifi.wd.patientsation.peripheral.mainapp.Adapter.CallDoctorIsTelInfoAdapter;
import doorwifi.wd.patientsation.peripheral.mainapp.Adapter.CallLineUpInfoAdapter;
import doorwifi.wd.patientsation.peripheral.mainapp.Adapter.illnessDoorAdapter;
import doorwifi.wd.patientsation.peripheral.mainapp.utils.DividerGridItemDecoration;
import doorwifi.wd.patientsation.peripheral.mainapp.utils.UIUtils;

import static java.lang.Thread.sleep;

/**
 * Created by xuhuan on 2017/7/6.
 * 呼叫医生
 */

public class CallDoctor extends Fragment implements illnessDoorAdapter.illnessDoorItemOnclickListener{

    @ViewInject(R.id.call_doctor_xrecyclerview)
    private XRecyclerView call_doctor_xrecyclerview;


    @ViewInject(R.id.call_line_up_info_listview)
    private ListView call_line_up_info_listview;

    @ViewInject(R.id.is_telephone_info_listview)
    private ListView is_telephone_info_listview;

    private LinearLayoutManager manager;
    private ArrayList<illnessRoom> roomList;
    private illnessRoom room;
    private doorwifi.wd.patientsation.peripheral.mainapp.Adapter.illnessDoorAdapter illnessDoorAdapter;
    private CallLineUpInfo lineUpInfo;
    private ArrayList<CallLineUpInfo> lineUpInfoList;
    private CallLineUpInfoAdapter lineUpinfoAdapter;
    private CallDoctorIsTelInfoAdapter callDoctorIsTelInfoAdapter;

    private Handler handle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.xh_call_doctor_layout,null);
        x.view().inject(this,view);
        initDoctor();//初始化门口机
        initCallLineUpInfo();//显示呼叫排队信息栏
        initIsTelInfo(); //初始化通话信息栏
        return view;

    }

    private void initIsTelInfo() {
        callDoctorIsTelInfoAdapter = new CallDoctorIsTelInfoAdapter(getActivity(),"108 109 110");
        is_telephone_info_listview.setAdapter(callDoctorIsTelInfoAdapter);
    }

    //获得呼叫排队信息栏数据
    private void initCallLineUpInfo() {
        getLineUpInfoData();
        lineUpinfoAdapter = new CallLineUpInfoAdapter(getActivity(),lineUpInfoList);
        call_line_up_info_listview.setAdapter(lineUpinfoAdapter);
    }

    public void getLineUpInfoData() {
        lineUpInfoList = new ArrayList<CallLineUpInfo>();
        for (int i = 3; i < 6; i++) {
            lineUpInfo = new CallLineUpInfo();
            lineUpInfo.setDate("2017-07-11");
            lineUpInfo.setTime("09:15:15");
            lineUpInfo.setIllnessRoom("102房");
            lineUpInfo.setIllnessBed(i + "房正在呼叫");
            lineUpInfoList.add(lineUpInfo);
        }
    }

    private void initDoctor() {
        getDoctorData();
        call_doctor_xrecyclerview.setLayoutManager(  new GridLayoutManager(getActivity(),3));
        illnessDoorAdapter = new illnessDoorAdapter(getActivity(),roomList);
        call_doctor_xrecyclerview.setAdapter(illnessDoorAdapter);
        call_doctor_xrecyclerview.addItemDecoration(new DividerGridItemDecoration(getActivity()));
        call_doctor_xrecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        call_doctor_xrecyclerview.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        call_doctor_xrecyclerview.setArrowImageView(R.drawable.loading_01);
        call_doctor_xrecyclerview.setPullRefreshEnabled(true);
        call_doctor_xrecyclerview.setLoadingMoreEnabled(true);

        illnessDoorAdapter.setillnessDoorOnItemClickListener(this);
        call_doctor_xrecyclerview.setLoadingListener(listener);


    }
    private void getDoctorData() {
        roomList = new ArrayList<illnessRoom>();
        for(int i = 1; i < 32; i ++){
            room = new illnessRoom();
            room.setIllnessNumber(i+"医生");
            roomList.add(room);
        }
    }

    private XRecyclerView.LoadingListener listener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            UIUtils.showToast(getActivity(),"--");
//            delay(5000);
            illnessDoorAdapter.notifyDataSetChanged();
            call_doctor_xrecyclerview.refreshComplete();

        }

        @Override
        public void onLoadMore() {
            UIUtils.showToast(getActivity(),"-11111-");
//            delay(5000);
            call_doctor_xrecyclerview.loadMoreComplete();
        }
    };


    private void delay(int ms){
        try {
            Thread.currentThread();
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void onSickDoorItemClick(View view, int position) {

    }
}
